package cn.micro.eye.commons.validate.validator;

import cn.micro.eye.commons.utils.TelUtils;
import cn.micro.eye.commons.validate.Tel;
import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * The mobile phone Constraint Validator
 *
 * @author lry
 */
@Slf4j
public class TelValidator implements ConstraintValidator<Tel, String> {

    @Override
    public void initialize(Tel tel) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || value.length() == 0) {
            return false;
        }

        if (TelUtils.validateMobile(value)) {
            return true;
        }

        return TelUtils.validatePhone(value);
    }

}